<?php
// 首页显示信息以及基本配置
namespace Partner\Controller;
use Think\Controller;
class StoreController extends CommonController{
    function myshop(){
        $uid=session('user_id');
        // $rs=M('partner')->where('u_id='.$uid)->find();
        $store	= D('store');
        $storeinfo = $store->where('uid='.$uid)->find();
        if($storeinfo){
            $this->assign('current',$storeinfo);
        }
        $this->display();
    }

    //店铺管理
    public function store_list(){
        $storeInfo = D('store');
        $title = $_GET['title'];
        $id=session('id');
        $user_info=M('partner')->find($id);
        $bids=explode(',',$user_info['b_id']);
        if($user_info['level']==1){
            $where['province']=$bids[0];
        }
        if($user_info['level']==2){
            $where['province']=$bids[0];
            $where['city']=$bids[1];
        }
        if($user_info['level']==3){
            $where['province']=$bids[0];
            $where['city']=$bids[1];
            $where['district']=$bids[2];

        }
        if($title === iconv('utf-8', "utf-8", iconv("utf-8", 'utf-8', $title))){
            $title=$title;
        }else{
            $title= iconv("gbk","utf-8",$title);
        }
        if($title){
            $where['title'] = array('like',"%$title%");
        }
        if($_GET['status']!=null){
            $where['status']=$_GET['status'];
        }
        if($_GET['recommend']){
            $where['recommend']=$_GET['recommend'];
        }
        if($_GET['addtime']&&$_GET['addtime1']){
            $addtime=strtotime($_GET['addtime']);
            $addtime1=strtotime($_GET['addtime1']);
            $where['addtime']=array('between',array($addtime,$addtime1));
        }
        if($_GET['addtime']==null&&$_GET['addtime1']){
            $addtime=strtotime($_GET['addtime']);
            $where['addtime']=array('ELT',$addtime);
        }
        if($_GET['addtime']&&$_GET['addtime1']==null){
            $addtime1=strtotime($_GET['addtime1']);
            $where['addtime']=array('EGT',$addtime1);
        }

        $count =$storeInfo->where($where)->count();
        $Page = new \Think\Page ($count, 15 ); // 实例化分页类 传入总记录数
        $show = $Page->show1(); // 分页显示输出
        $storeArray = $storeInfo->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page', $show ); // 赋值分页输出
        $this->assign('count',$count);
        $this->assign('storeArray', $storeArray );
        $this->assign('title', '店铺列表' );
        $this->display();
    }
    function shopadd(){
        $uid=session('user_id');
        $storeInfo	= D('store');
        $id = I('get.id',0);
        $store_classification=M('store_classification');
        $store_classifications=$store_classification->select();
        $this->assign('store_classifications',$store_classifications);
        if($id){
            $current = $storeInfo->where('id='.$id)->find();
            $user=M('users')->find($current['uid']);
            $u_id=session('u_id');
            if($u_id==$current['uid']){
                $this->assign ( 'type',1 );
            }else {
                $this->assign ( 'type',0 );
            }
            $this->assign ( 'title', '编辑品牌店' );
            $this->assign ( 'current',$current );
            $this->assign ( 'user',$user );
        }else{
            $this->assign ( 'type',1 );
            $this->assign ( 'title', '添加品牌店' );
        }
        $this->display();
    }
    public function search_phone(){
        $phone=I('get.phone');
        $users=M('users');
        $rs=$users->where('phone='.$phone)->find();
        if($rs){
            $store=M('store');
            $c_rs=$store->where('uid='.$rs['id'])->select();
            if($c_rs){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn($rs);
            }
        }else{
            $this->ajaxReturn(0);
        }

    }
    public function store_Save(){
        $data=I('post.');

        $data['store_classification_type']=implode(',',$data['store_classification_type']);
        if($data['extraction']){
            $data['extraction']=$data['extraction']/100;
        }
        if($data['spread']=='on'){
            $data['spread']=1;
        }else{
            $data['spread']=0;
        }
        if($data['recommend']=='on'){
            $data['recommend']=1;
        }else{
            $data['recommend']=0;
        }
        if($data['status']=='on'){
            $data['status']=1;
        }else{
            $data['status']=0;
        }
        if($data['quanwang']=='on'){
            $data['quanwang']=1;
        }else{
            $data['quanwang']=0;
        }
        $storeInfo	= D('store');
        if($data['id']){
            $storeInfo->save($data);
            echo "0|||更新成功|||reload";
        }else{
            $data['addtime']=time();
            $data['password']=passport_encrypt($data['password'],'wnsc');
            $storeInfo->add($data);
            echo "0|||添加成功|||reload";
        }
    }
    //修改状态
    public function storeSave(){
        $data=I('get.');
        $type=$data['type'];
        $storeInfo = D('store');
        $store_info=$storeInfo->find($data['id']);
        if($store_info[$type]==0){
            $info[$type]=1;
            $info['id']=$data['id'];
        }else{
            $info[$type]=0;
            $info['id']=$data['id'];
        }
        $rs=$storeInfo->save($info);
        if($rs){
            die('0|||操作成功|||/Store/store_list');
        }
    }
    public function upload(){
        if(I('get.isthumb')){
            $width  =   I('get.width','400');
            $height =   I('get.height','400');
        }else{
            $width  =   I('get.width');
            $height =   I('get.height');
        }
        if (!empty($_FILES)) {
            import("@.ORG.UploadFile");
            $upload = new \Think\Upload();
            $upload->maxSize = 2048000;
            $upload->allowExts = array('jpg','jpeg','gif','png','xls');
            $upload->savePath = "/";
            $upload->thumb = true; //设置缩略图
            $upload->imageClassPath = "@.ORG.Image";
            $upload->saveRule = uniqid; //上传规则
            $upload->thumbRemoveOrigin = true; //删除原图
            $info   =   $upload->upload();
            if(!$info) {
                echo 'error'.$upload->getError();// 上传错误提示错误信息
            }else{
                foreach($info as $file){
                    $path='/Uploads'.$file['savepath'].$file['savename'];
                    $name   =   $_SERVER["DOCUMENT_ROOT"].$path;
                    $imageArray =   getimagesize($name);
                    if( ( $width || $height ) && ( $imageArray[0] > $width || $imageArray[1] > $height ) ){
                        $save_path=$_SERVER['DOCUMENT_ROOT'].$path;
                        $thumb_path=$_SERVER['DOCUMENT_ROOT'].'/Uploads'.$file['savepath'].'thumb_'.$file['savename'];
                        $image = new \Think\Image();
                        $image->open($save_path);
                        $image->thumb($width, $height)->save($save_path);
                    }

                }
            }

            $data['saveURL']=$path;
            $data['state']='上传成功！';
            $data['code']=1;
            $this->ajaxReturn($data);

        }

    }
    /**
     * 获取省列表
     */
    public function getProvince()
    {
        $id=session('id');
        $user_info=M('partner')->find($id);
        $bids=explode(',',$user_info['b_id']);
        $province_id=$bids[0];

        $sys_province=M('sys_province');
        $list = $sys_province->where( 'province_id='.$province_id)->order('sort asc')->select();
        $this->ajaxReturn($list);
    }

    /**
     * 获取城市列表
     *
     * @return Ambigous <multitype:\think\static , \think\false, \think\Collection, \think\db\false, PDOStatement, string, \PDOStatement, \think\db\mixed, boolean, unknown, \think\mixed, multitype:, array>
     */
    public function getCity()
    {
        $id=session('id');
        $user_info=M('partner')->find($id);
        $bids=explode(',',$user_info['b_id']);
        $city_id=$bids[1];
        if($user_info['level']>1){

            $sys_city=M('sys_city');
            $city_list = $sys_city->where( 'city_id='.$city_id)->order('sort asc')->select();

        }else{
            $province_id = isset($_POST['province_id']) ? $_POST['province_id'] : 0;
            if($province_id!=0){
                $city_list = getCityList($province_id);
            }else{
                $city_list=null;
            }


        }
        $this->ajaxReturn($city_list);

    }

    /**
     * 获取区域地址
     */
    public function getDistrict()
    {
        $id=session('id');
        $user_info=M('partner')->find($id);
        $bids=explode(',',$user_info['b_id']);
        $district_id=$bids[2];
        if($user_info['level']>2){
            $sys_district=M('sys_district');
            $district_list = $sys_district->where( 'district_id='.$district_id)->order('sort asc')->select();
        }else{

            $city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;
            if($city_id!=0){
                $district_list = getDistrictList($city_id);
            }else{
                $district_list=null;
            }
        }
         $this->ajaxReturn($district_list);

    }

    /**
     * 获取选择地址
     *
     * @return unknown
     */
    public function getSelectAddress()
    {
        $address = new Address();
        $province_list = getProvinceList();
        $province_id = isset($_POST['province_id']) ? $_POST['province_id'] : 0;
        $city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;
        $city_list = getCityList($province_id);
        $district_list =getDistrictList($city_id);
        $data["province_list"] = $province_list;
        $data["city_list"] = $city_list;
        $data["district_list"] = $district_list;
        $this->ajaxReturn($data);
    }

    public function store_edit_ali(){
        $id=$_GET['id'];
        $storeInfo	= D('store');
        $current = $storeInfo->find($id);
        $this->assign ('current',$current );
        $this->display();
    }



}